package be.phl.android.project;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class OverzichtActivity extends ListActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		String[] items = getResources().getStringArray(R.array.OverzichtItems);
		setListAdapter(new ArrayAdapter<String>(this, R.layout.overzicht, items));

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent i;
				JsonHandler handler = JsonHandler.getInstance();
				handler.setContext(getApplicationContext());

				switch (position) {
				case 0:
					try {
						i = new Intent(view.getContext(),
								LessenroosterActivity.class);
						i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
						startActivityForResult(i, 0);
					} catch (Exception e) {
						Toast.makeText(view.getContext(), e.toString(),
								Toast.LENGTH_SHORT);
					}
					break;

				case 1:
					try {
						i = new Intent(view.getContext(), DagmenuActivity.class);
						i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
						startActivityForResult(i, 0);
					} catch (Exception e) {
						Toast.makeText(view.getContext(), e.toString(),
								Toast.LENGTH_SHORT);
					}
					break;
				case 2:
					try {
						handler.update();
						Toast.makeText(getApplicationContext(),
								handler.getLastUpdate(), Toast.LENGTH_SHORT)
								.show();
					} catch (Exception e) {
						Toast.makeText(view.getContext(), e.toString(),
								Toast.LENGTH_SHORT);
					}
					break;
				default:
					break;
				}
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.overzichtmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.preferences:
			Intent i = new Intent(OverzichtActivity.this,
					PreferencesActivity.class);
			startActivity(i);
			break;
		}
		return true;
	}
}
